package com.mai4j.maiadmin.validation.constraintvalidators;


import com.mai4j.maiadmin.validation.constraints.Option;
import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Arrays;

/**
 * Created by 席有芳 on 2021/12/15.
 *
 * @author 席有芳
 */
public class OptionValidator implements ConstraintValidator<Option, String> {

    private String[] options;

    @Override
    public void initialize(Option annotation) {
        this.options = annotation.options();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (StringUtils.isBlank(value)) {
            return true;
        }
        return options != null && Arrays.asList(options).contains(value);
    }
}
